package com.fy.fyspace.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Component
public class EmailCodeSend {

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 打包邮件验证码内容
     *
     * @param email 收件人邮箱
     * @param code  验证码
     * @return 邮件消息对象
     */
    public MimeMessage packageMessage(String email, String code, int codeOutTime) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

        helper.setFrom("Yun盒子<" + "2285114622@qq.com" + ">");
        helper.setTo(email);
        helper.setSubject("Yun盒子-安全验证");

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 将秒转换为分钟并设置过期时间
        int minutes = codeOutTime / 60;
        LocalDateTime expireTime = now.plusMinutes(minutes);
        String expireTimeStr = expireTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));

        // 构建HTML格式的邮件内容
        String content = String.format(
                "<div style='font-family: Arial, sans-serif; color: #333;'>" +
                        "<p>收到了您的验证码请求，为了保护您的账户安全，请使用以下验证码完成验证：</p>" +
                        "<div style='background: #f8f9fa; padding: 15px; border-radius: 5px; margin: 20px 0; text-align: center;'>"
                        +
                        "<h1 style='color: #e74c3c; font-size: 24px; margin: 0;'>验证码：<strong>%s</strong></h1>" +
                        "</div>" +
                        "<p style='color: #7f8c8d;'>• 验证码有效期至：<span style='color: #e67e22;'>%s</span></p>" +
                        "<div style='background: #f5f6fa; padding: 15px; border-left: 4px solid #3498db; margin: 20px 0;'>"
                        +
                        "<h3 style='color: #2980b9; margin-top: 0;'>[安全提示]</h3>" +
                        "<p style='color: #e74c3c;'>* 请勿将验证码泄露给他人</p>" +
                        "<p style='color: #e74c3c;'>* 如非本人操作,请及时修改密码!</p>" +
                        "</div>" +
                        "<hr style='border: 1px solid #eee;'>" +
                        "<p>如您在使用过程中遇到问题欢迎随时联系我们：</p>" +
                        "<p>• 客服邮箱：<a href='mailto:%s' style='color: #3498db;'>%s</a></p>" +
                        "<p style='color: #7f8c8d;'>• 服务时间：周一至周日 09:00-21:00</p>" +
                        "<p><em>感谢您的使用，非常非常非常非常感谢！</em></p>" +
                        "<hr style='border: 1px solid #eee;'>" +
                        "<p style='color: #95a5a6; font-size: 12px;'>本邮件由系统自动发送，请勿回复...</p>" +
                        "<p style='color: #95a5a6; font-size: 12px;'><em>Yun盒子 .auth@fwq</em></p>" +
                        "</div>",
                code, expireTimeStr, "2285114622@qq.com", "2285114622@qq.com");

        helper.setText(content, true); // true表示支持HTML
        log.info("正在发送验证码邮件至: {}", email);

        return mimeMessage;
    }
}